﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace nr.GLV.Windows.Controls
{
    public enum FieldType
    {
        Text = 0,
        Numeric = 1,
        Date = 2
    }

    public class FilterField : ISerializable
    {
        public string FriendlyName { get; set; }
        public string FieldName { get; set; }
        public FieldType FieldType { get; set; }
        public string Value { get; set; }

        public FilterField() : this(string.Empty, string.Empty) { }

        public FilterField(string fieldName) : this(fieldName, fieldName) { }

        public FilterField(string friendlyName, string fieldName) : this(friendlyName, fieldName, FieldType.Text) { }

        public FilterField(string friendlyName, string fieldName, FieldType fieldType)
        {
            FriendlyName = friendlyName;
            FieldName = fieldName;
            FieldType = fieldType;
        }

        public FilterField(SerializationInfo si, StreamingContext context)
        {
            FriendlyName = si.GetString("text");
            FieldName = si.GetString("name");
            FieldType = (FieldType)si.GetValue("type", typeof(FieldType));
            Value = si.GetString("value");
        }

        public string FilterText
        {
            get
            {
                StringBuilder result = new StringBuilder(FieldName);
                result.Append(" {0} ");
                switch (FieldType)
                {
                    case Controls.FieldType.Text:
                        result.Append("'{1}'");
                        break;
                    case Controls.FieldType.Numeric:
                        result.Append("{1}");
                        break;
                    case Controls.FieldType.Date:
                        result.Append("#{1}#");
                        break;
                }
                return result.ToString();
            }
        }

        public string FilterFriendlyFormat
        {
            get
            {
                return FriendlyName + " {0} «{1}»";
            }
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("text", FriendlyName);
            info.AddValue("name", FieldName);
            info.AddValue("type", FieldType, typeof(FieldType));
            info.AddValue("value", Value);
        }
    }
}
